home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / NextDeveloper / Source / GNU / cc / listing < prev    next >
Text File  |  1993-11-29  |  4KB  |  160 lines

  1. #!/bin/sh -f
  2. # Generate a source code listing for C or C++ code with assembler code. The
  3. # listing is always written to stdout.
  4. # Author: Igor Metz <metz@iam.unibe.ch>
  5.  
  6. # Revision 1.3  89/12/18  13:58:27  metz
  7. # lister must now be configured before it can be used. This is done in the
  8. # /bin/sh part of the code.
  9. # Revision 1.2  89/08/16  17:35:02  metz
  10. # Support for SPARC added.
  11. # Revision 1.1  89/08/16  16:49:22  metz
  12. # Initial revision
  13.  
  14. # Requires: gawk (may be it works also with nawk)
  15.  
  16. # usage:  lister filename [compiler-options]
  17.  
  18. # Method:
  19. # compile the source with -g option to assembler code, then merge the
  20. # generated assembler code with the source code. Compiler options
  21. # can be supplied on the command line (for example -O)
  22.  
  23. # To install lister, assign one of the supported values to the variable MYSYS:
  24. # mc68020  for Motorola 68020 (Sun-3, ..)
  25. # mc68030  for Motorola 68030 (Sun-3, ..)
  26. # sparc    for SPARC (SUN-4, ..)
  27. # i386     for i386 (Sun i386, ...)
  28. # i386-linux for i386 (Linux, ...)
  29.  
  30. # uncomment the line you need:
  31. # MYSYS=mc68020
  32. # MYSYS=mc68030
  33. # MYSYS=sparc
  34. # MYSYS=i386
  35. # MYSYS=i386-linux
  36. # MYSYS=`mach`  # this will work on Suns with SunOS > 4.0.0
  37.  
  38. FILENAME=$1
  39. shift
  40.  
  41. exec gawk -vsys=$MYSYS -voptions="$*" '
  42. # commandline arguments:
  43. #  ARGV[0] = "gawk"
  44. #  ARGV[1] = processid
  45. #  ARGV[2] = filename
  46. BEGIN {
  47.   if (ARGC != 3) {
  48.     usage()
  49.     exit 1
  50.   }
  51.  
  52.   # Declaration of global variables
  53.   c_filename = ""
  54.   asm_filename = ""
  55.   cmdline = ""
  56.   asm_code = ""
  57.   c_code = ""
  58.   c_lineno = 0
  59.   oldlineno = 0
  60.   newlineno = 0
  61.   ignore_stabd = 0
  62.   num_of_fields = 0
  63.  
  64.   # check processor architecture and set sourcecode line_hint accordingly
  65.   if (sys == "sparc" || sys == "i386") {
  66.     line_hint = "^[ \t]*\.stabn.*"
  67.   }
  68.   else if (sys == "mc68020" || sys == "mc68030" || sys == "i386-linux") {
  69.     line_hint = "^[ \t]*\.stabd.*"
  70.   }
  71.   else {
  72.     error("Processor type " sys " is not supported yet, sorry")
  73.   }
  74.  
  75.   parse_cmdline()
  76.  
  77.   printf("compiling %s to asm code\n", c_filename ) > "/dev/stderr"
  78.  
  79.   if (system(cmdline) != 0 ) {
  80.     error("Compilation of " c_filename " failed")
  81.   }
  82.  
  83.   printf("generating listing\n") > "/dev/stderr"
  84.  
  85.  
  86.   while ( getline asm_code < asm_filename > 0 ) {
  87.     if ( (ignore_stabd==0) && (asm_code ~ line_hint)) {
  88.       # source line hint found. Split the line into fields separated by commas.
  89.       # num_of_fields is 4 for sparc, 3 for m68k
  90.       num_of_fields = split(asm_code, fields, ",")
  91.       newlineno = fields[3] + 0 # the line number we are looking for is field 3
  92.  
  93.       if (newlineno > oldlineno) {
  94.         while ( newlineno > c_lineno ) {
  95.       getline c_code < c_filename
  96.       c_lineno++
  97.       printf("%4d %s\n", c_lineno, c_code)
  98.     }
  99.     oldlineno = newlineno
  100.       }
  101.     }
  102.     else if ( asm_code ~ ".*Ltext[ \t]*$" ) {
  103.       # filename hint found
  104.       if ( match(asm_code, c_filename)) {
  105.         ignore_stabd = 0
  106.       }
  107.       else {
  108.         ignore_stabd = 1
  109.       }
  110.     }
  111.     printf("\t\t\t%s\n", asm_code)
  112.   }
  113.  
  114.   # general cleanup
  115.   system("/bin/rm " asm_filename)
  116. }
  117.  
  118. function usage() {
  119.     printf("usage: %s filename compiler-options\n", argv[0]) > "/dev/stderr"
  120. }
  121.  
  122. function error(s) {
  123.     printf("error: %s\n", s) > "/dev/stderr"
  124.     exit 1
  125. }
  126.  
  127. function parse_cmdline(    i) {
  128.   # construct filenames to use
  129.   asm_filename = "/tmp/lister" ARGV[1] ".s"
  130.   ARGV[1] = ""
  131.   c_filename = ARGV[2]
  132.   ARGV[2] = ""
  133.  
  134.   # construct commandline to use
  135.   if ( match(c_filename, ".C") || match(c_filename, ".cc") ) {
  136.     cmdline = "g++"
  137.   }
  138.   else if (match(c_filename, ".c")) {
  139.     cmdline = "gcc"
  140.   }
  141.   else {
  142.     error("unknown extension for file " c_filename)
  143.   }
  144.  
  145.   cmdline = cmdline " -g -S -o " asm_filename
  146.  
  147.   # now we append the compiler options specified by the user
  148.   cmdline = cmdline " " options
  149.  
  150.   # last but not least: the name of the file to compile
  151.   cmdline = cmdline " " c_filename
  152. }
  153.  
  154. ' $$ $FILENAME
  155.  
  156.